package com.cise.services.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;

import com.cise.searcher.AbstractParallelSearcher;
import com.cise.searcher.ParallelSearcherFactory;
import com.cise.searcher.utils.Result;
import com.cise.services.SearchService;
import com.cise.utils.Util;

import net.semanticmetadata.lire.ImageSearchHits;
import net.semanticmetadata.lire.ImageSearcher;
import net.semanticmetadata.lire.ImageSearcherFactory;
import net.semanticmetadata.lire.SearchHitsFilter;

public class SearchServiceImpl implements SearchService {
	
	private File file = null;
	private double[] featureWeight = null;

	public SearchServiceImpl(File file) {
		this.file = file;
	}
	
	@Override
	public List<Result> search() {
		if(null == file || !file.exists() || !Util.isImage(file)){
			return null;
		}
		
		AbstractParallelSearcher aps;
		if(this.featureWeight != null){
			aps = ParallelSearcherFactory.
				createTestLinearParallelSearcher(Util.SELECTED_ALGORITHMS, this.featureWeight);
		}
		else{
			aps = ParallelSearcherFactory.createParallelSearcher(Util.SELECTED_ALGORITHMS);
		}
		
		BufferedImage img = null;
		try {
			img = ImageIO.read(file);
			
			List<Result> results = aps.search(img);
			return results;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public void setFeatureWeight(double[] featureWeight) {
		this.featureWeight = featureWeight;
	}
	
}
